package com.test.demo1.controller;

import com.test.demo1.bean.Items;
import com.test.demo1.common.PageRequest;
import com.test.demo1.common.RedissonInventoryManager;
import com.test.demo1.common.ResponseResult;
import com.test.demo1.service.ItemsService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * (Items)表控制层
 *
 * @author makejava
 * @since 2025-08-31 08:11:35
 */
@RestController
@RequestMapping("items")
@Slf4j
public class ItemsController {
    /**
     * 服务对象
     */

    private final ItemsService itemsService;
    private final RedissonInventoryManager manager;

    public ItemsController(ItemsService itemsService, RedissonInventoryManager manager) {
        this.itemsService = itemsService;
        this.manager = manager;
    }


    /**
     * 分页查询
     *
     * @param items       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @PostMapping("queryAll")
    public List<Items> queryAll(@RequestBody Items items) {
        return this.itemsService.queryAll(items);
    }

    /**
     * 分页查询
     *
     * @param items       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public PageInfo<Items> queryByPage(@RequestBody PageRequest<Items> items) {
        return this.itemsService.queryByPage(items);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseResult<Items> queryById(@PathVariable("id") BigInteger id) {
        return ResponseResult.ok(this.itemsService.queryById(id));
    }

    /**
     * 查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/query")
    public ResponseResult<Items> queryById(@RequestBody Items items) {
        return ResponseResult.ok(this.itemsService.selectOne(items));
    }

    /**
     * 新增数据
     *
     * @param items 实体
     * @return 新增结果
     */
    @PostMapping("update")
    public ResponseResult<Items> saveOrUpdate(@RequestBody Items items) {
        this.itemsService.updateById(items);
        return ResponseResult.ok();
    }
    /**
     * 新增数据
     *
     * @param items 实体
     * @return 新增结果
     */
    @PostMapping("init")
    public void init() {
        Items items = this.itemsService.selectOne(Items.builder().id(BigInteger.ONE).build());
        if (items!=null){

            manager.initInventorySegments(items.getQty());
        }

    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("delete")
    public ResponseResult<Boolean> deleteById() {
        boolean success = manager.deductInventory(1);
        if (success){
            log.info(Thread.currentThread().getName()+"库存扣减成功");
        }else{
            log.info(Thread.currentThread().getName()+"库存扣减失败");
        }
        return ResponseResult.ok();
    }

}

